import Vue from 'vue'
import Router from 'vue-router'
import jsCookies from 'js-cookie'
import store from '../store'
import page404 from '@/views/error/404'
import login from '@/views/login'
import person from '@/views/user/person'
import collectionList from '@/views/device/collection-list'
import switchList from '@/views/device/switch-list'
import sceneList from '@/views/scene/scene-list.vue'
import sceneSwitch from '@/views/scene/scene-switch.vue'
import sceneSwitchAdd from '@/views/scene/scene-switch-add.vue'

Vue.use(Router)


const routerConfig = {
    routes: [
        {
            path: '/login',
            component: login
        },
        {
            path: '/person',
            component: person
        },
        {
            path:'/collection',
            component:collectionList,
            meta: {
                keepAlive: true
            }
        },
        {
            path:'/switch',
            component:switchList,
            meta: {
                keepAlive: true
            }
        },
        {
            path:'/scene',
            component: sceneList
        },
        {
            path:'/sceneSwitch/:id',
            component:sceneSwitch
        },
        {
            path:'/sceneSwitchAdd/:id',
            component:sceneSwitchAdd
        },
        {
            path: '/*',
            component: page404
        },

    ]
};


const router = new Router(routerConfig);
router.beforeEach((to, from, next) => {
  if (!sessionStorage.getItem("login")&& to.path !== '/login') {  // 判断是否已经登录且前往的页面不是登录页
    next({
      path: '/login'
    });
  } else if (sessionStorage.getItem("login") && to.path === '/login') {  // 判断是否已经登录且前往的是登录页
      next({
      path: '/switch'
    });
  } else {
    next();
  }
});
router.afterEach((to)=>{
    store.commit("SET_CURRENT_PATH",to.path)
});
export default router
